// ================================================================================
// 		File: PartyGrassrootsGroup.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-06-11 23:56
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Grassroots
{
    /// <summary>
    /// 驻点分组
    /// </summary>   
    public class PartyGrassrootsGroup : EntityBase
    {
        #region 属性
        /// <summary>
        /// 组织ID
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 分组（党委）
        /// </summary>
        public string GroupName { get; set; }

        /// <summary>
        /// 页面展示偏移量X
        /// </summary>
        public int CssPointX { get; set; }

        /// <summary>
        /// 页面展示偏移量Y
        /// </summary>
        public int CssPointY { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 状态(1-草稿，4-发布)
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        public int? OrderNo { get; set; }

        #endregion

        #region 查询管理

        public static DataSet GetGridData(int pageIndex, int pageSize, string searchKey = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  data.ID,data.BranchID,data.GroupName,data.CssPointX,data.OrderNo,
data.CssPointY,data.Description,data.State,data.CreateBy,data.CreateTime,data.ModifyTime,data.ModifyBy,if(State=4,'发布','草稿') as StateName
                        from tblPartyGrassrootsGroup data
                        where 1=1 ");

            sb.Append(" order by data.OrderNo asc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetGroupData(string state = "", string branchID = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>();

            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT data.ID,data.GroupName,data.CssPointX,
data.CssPointY,data.State,data.Description,data.CreateBy,
data.CreateTime,data.ModifyBy,data.ModifyTime,
case data.State when 1 then '已发布' else '草稿' end as StateName from tblpartygrassrootsgroup data
                                 where 1=1");

            if (!string.IsNullOrEmpty(state))
            {
                sb.Append(" and data.state=?state");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "state", Value = state });
            }
            if (!string.IsNullOrEmpty(branchID))
            {
                sb.Append(@" and data.ID in (select GroupID from  tblpartygrassrootsgroupitem 
where tblpartygrassrootsgroupitem.BranchID in  (select childbranchid from tblpartyorgbranchrelation where BranchID=?BranchID))");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchID });
            }

            sb.Append(" order by data.CreateTime desc");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataTable GetGroupItemData()
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>();

            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT * FROM (SELECT ID,'' as ParentID,GroupName AS Name ,'' as TypeName,'' TypeID ,OrderNo
                        FROM tblpartygrassrootsgroup WHERE State=4 ORDER BY OrderNo ASC) as t
                        UNION
                        SELECT * from(SELECT i.ID,i.GroupID AS ParentID,i.ItemName  AS Name,ifnull(t.TypeName,'管区') AS TypeName,
                        ifnull(t.ID,'Null') AS TypeID,i.OrderNo FROM tblpartygrassrootsgroupitem i
                        left join tblpartygrassrootsgroupitemtype t on (i.TypeID=t.id)
                         WHERE State=4 ORDER BY t.OrderNo ASC,i.OrderNo ASC) as tt");

            //sb.Append(" order by CreateTime desc");
            return MySqlHelper.ExecuteDataTable(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
